<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	//Ajout du plugin DebugKit
	public $components = array('DebugKit.Toolbar', 'Session');

	public function changeLanguageTo($language){
		
		$language_session=$this->Session->read('User.language');

		//Si language nouveau language different de celui en session, on modifie celui de la session
		if($language_session!=$language){
			$this->Session->write('User.language', $language);
			Configure::write('Config.language', $this->Session->read('User.language'));	
			//$this->Session->setFlash(__('Language modified to ').$language);
			$this->showAlert(__('Language modified to ').$language, 'success');
		}
		
		//retour sur la page actuelle
		return $this->redirect($this->referer());
	}


	function beforeFilter(){
		
		//Changement de la langue
		$language_session=$this->Session->read('User.language');
		if( isset($language_session) && in_array($language_session, Configure::read('Config.languages'))){
			Configure::write('Config.language', $this->Session->read('User.language'));			
		}
		else{
			//Langage par défaut
			$this->Session->write('User.language', Configure::read('Config.language'));
		}		
	}

	//alert stylisée
	public function showAlert($content, $type_alert){
		$content='<div class="container">
						<div class="row clearfix">
							<div class="col-md-12 column">
								<div class="alert alert-' . $type_alert . ' alert-dismissable">
									 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> <h4>Alert!</h4>' . $content . '</div></div></div></div>';
		$this->Session->setFlash($content);
	}
	
}
